﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace mark
{
    class parse
    {
       
        public string[] spisok = new string[200];
        public int order = 0;
        public DateTime[] d = new DateTime[200];
        public string[] list(string uristr,ref int n)
        {
            string[] mas = new string[200];

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uristr);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream istrm = resp.GetResponseStream();
            StreamReader rdr = new StreamReader(istrm);
            string str = rdr.ReadToEnd();

            strwork c = new strwork();
            int loc = 0; string be, en;
            be = @"rel=""tooltip""  />" + "\n" + "						<a href=";

            en = "</a>";
            string s2 = "12345", prevs2 = "00000" ;
            //string[] mas = new string[1000];
            n = 0;

            while ((s2 != null) && (s2.Length == 5))
            {
                s2 = c.between(str, be, en, ref  loc, 24+16);
                if (s2 != prevs2)
                {
                    mas[n] = s2;
                    n++;
                }
                prevs2 = s2;
            }
            spisok = mas;
            return mas;

        }

        public DateTime[] timetable(string uristr, ref int n)
        {
            string[] mas = new string[200];
            mas = list(uristr, ref n);
            DateTime[] d = new DateTime[200];
            n--;
            for (int i = 0; i < n; i++)
                d[i]=Convert.ToDateTime(mas[i]);
            return d;
        }

        public void init_nearest(string uristr)
        {
            parse t = new parse();
            int n = 0;
            d = t.timetable(uristr, ref n);
            DateTime q = DateTime.Now;
            int i = 0, u = 0;
            while ((i < 190) && (d[i] < q)) i++;
        }

        public string nearest()
        {
            DateTime q = DateTime.Now;
            int i = 0;
            while ((i < 190) && (d[i] < q)) i++;
            if ((order == 0) && (i == 190)) return "Cегодня нет";
            if (i == 190) return "";
            if (d[i + order].Hour.ToString() == "") return "";
            else 
            return d[i+order].Hour.ToString() + @":" + d[i+order].Minute.ToString();
        }
    }
}
